function bnd=newboundary(nodes,varargin)
% newboundary: Create a new boundary object
%
% SYNTAX:
%   bnd = newboundary(nodes,prop1,value1,...)
%   Return a MADS.Boundary object bnd via specified vertex nodes and properties
%   Type "newboundary([])" to see available properties and their default values. 
%   You can use function DRAWBOUNDARY to draw the boundary object.
%   
% See also NEWBOBSTACLE, DRAWBOUNDARY

%   Author: Hongbin Ma
%   Last updated: 2009-6-2 with MATLAB 6.5
%   Problems or suggestions? Email me: mathmhb[at]163.com

bnd=struct('type','MADS.Boundary',varargin{:});
bnd.nodes=nodes;
bnd=set_field(bnd,'pos',zeros(1,2));
bnd=set_field(bnd,'ang',zeros(1,2));
bnd=set_field(bnd,'shape','polygon');
bnd=set_field(bnd,'size',1);
bnd=set_field(bnd,'linestyle','-');
bnd=set_field(bnd,'linewidth',4);
bnd=set_field(bnd,'color',[0 1 0]);
if isnumeric(nodes) & ~isempty(nodes),
    bnd.convhull=convhull(nodes(:,1),nodes(:,2));
    return;
end;
switch bnd.shape,
case 'triangle'
    angles=rand(3,1)*pi*2-pi;
    angles=sort(angles);
case 'rect'
    angles=rand(4,1)*pi*2-pi;
    angles=sort(angles);
case 'polygon'
    n=floor(15*rand(1))+3;
    angles=rand(n,1)*pi*2-pi;
    angles=sort(angles);
end;
n=length(angles);
bnd.nodes=bnd.size*[cos(angles),sin(angles)];
bnd.convhull=convhull(bnd.nodes(:,1),bnd.nodes(:,2));
